package api

import (
	Tools2 "Wechat/Tools"
	service2 "Wechat/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 引入一个ActivityService用于引用函数
var activityservice service2.ActivityService

// 添加活动
func AddActivity(c *gin.Context) {
}

// 单个活动详情
func GetDetail(c *gin.Context) {
	//	 读取页面数据：form-data
	//activityID := c.DefaultPostForm("activityID", "")
	// 读取页面数据：json
	var activityMap = make(map[string]interface{})
	c.BindJSON(&activityMap)
	activityID := activityMap["activityID"].(string)
	// 返回数据
	var res = make(map[string]interface{})
	if activityID == "" {
		res["msg"] = "Fail"
		res["ERROR"] = "activityID is invalid!"
	} else if isOk, detail := activityservice.GetActivityDetail(activityID); isOk {
		// 调用自写方法MergeMap，合成两个Map
		res["msg"] = http.StatusText(http.StatusOK)
		res = Tools2.MergeMap(res, detail)
	} else {
		res["msg"] = "Fail"
		res["ERROR"] = "activityID NOT found!"
	}

	c.JSON(http.StatusOK, res)
	return
}

// 获取活动列表
func GetList(c *gin.Context) {
	//	 获取页面数据，并且将其转化为int64
	//activityIndex, err := strconv.ParseInt(c.DefaultPostForm("index", "0"), 10, 64)
	var activityMap = make(map[string]interface{})
	c.BindJSON(&activityMap)
	activityIndex, err := strconv.ParseInt(activityMap["index"].(string), 10, 64)
	//	检查错误
	Tools2.CherrError(err, "string to int64 ERROR!")
	isOK, actArr := activityservice.GetListActivity(activityIndex)
	if isOK {
		res := make(map[string]interface{})
		res["msg"] = http.StatusText(http.StatusOK)
		res["data"] = actArr
		c.JSON(http.StatusOK, res)
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "ok",
			"ERROR": "Without activity data!",
		})
	}
	return
}
